
$('#cbbNV').change(function() {
	$('#loading').show();
	$.get('/phanquyen/json/?func=getQuyen&type=2&ma=' + $('#cbbNV').val(), function(data){
		var select = $('#cbbNvAllowedFunc');
		select.find('option').remove();
		
		var select1 = $('#cbbNvDeniedFunc');
		select1.find('option').remove();
		var data = JSON.parse(data);
		var obj = data.lstAllowedFunc;
		for (var i=0; i < obj.length; i++)
		{
		  var option = document.createElement("option");
		  option.value = obj[i];
		  option.innerHTML = obj[i];
		  select.append(option);
		}
		
		obj = data.lstDeniedFunc;
		for (var i=0; i < obj.length; i++)
		{
		  var option = document.createElement("option");
		  option.value = obj[i];
		  option.innerHTML = obj[i];
		  select1.append(option);
		}
		$('#loading').hide();
	});
});

$('#cbbLoaiNV').change(function() {

	$('#loading').show();
	$.get('/phanquyen/json/?func=getQuyen&type=1&ma=' + $('#cbbLoaiNV').val(), function(data){
		var select = $('#cbbLnvAllowedFunc');
		select.find('option').remove();
		
		var select1 = $('#cbbLnvDeniedFunc');
		select1.find('option').remove();
		
		var data = JSON.parse(data);
		var obj = data.lstAllowedFunc;
		for (var i=0; i < obj.length; i++)
		{
		  var option = document.createElement("option");
		  option.value = obj[i];
		  option.innerHTML = obj[i];
		  select.append(option);
		}
		
		obj = data.lstDeniedFunc;
		for (var i=0; i < obj.length; i++)
		{
		  var option = document.createElement("option");
		  option.value = obj[i];
		  option.innerHTML = obj[i];
		  select1.append(option);
		}
		$('#loading').hide();
	});
});

$('#lnvLeftToRight').click(function() {
	var perm = $('#cbbLnvAllowedFunc').val();
	if (perm == null)
		return;
	var select = $('#cbbLnvAllowedFunc');
	
	var select1 = $('#cbbLnvDeniedFunc');

	$('#loading').show();
	$.get('/phanquyen/json/?func=removeQuyen&type=1&ma=' + $('#cbbLoaiNV').val() + '&perm=' + perm, function(data){
		if (data == 1) {
			$("#cbbLnvAllowedFunc option:selected").remove();
			var option = document.createElement("option");
			option.value = perm;
		  	option.innerHTML = perm;
		  	select1.append(option);
		}
		$('#loading').hide();
	});
});

$('#lnvRightToLeft').click(function() {
	var perm = $('#cbbLnvDeniedFunc').val();
	if (perm == null)
		return;
	var select = $('#cbbLnvAllowedFunc');
	
	var select1 = $('#cbbLnvDeniedFunc');
	
	$('#loading').show();
	$.get('/phanquyen/json/?func=addQuyen&type=1&ma=' + $('#cbbLoaiNV').val() + '&perm=' + perm, function(data){
		if (data == 1) {
			$("#cbbLnvDeniedFunc option:selected").remove();
			var option = document.createElement("option");
			option.value = perm;
		  	option.innerHTML = perm;
		  	select.append(option);
		}
		$('#loading').hide();
	});
});

$('#nvLeftToRight').click(function() {
	var perm = $('#cbbNvAllowedFunc').val();
	if (perm == null)
		return;
	var select = $('#cbbNvAllowedFunc');
	
	var select1 = $('#cbbNvDeniedFunc');

	$('#loading').show();
	$.get('/phanquyen/json/?func=removeQuyen&type=2&ma=' + $('#cbbNV').val() + '&perm=' + perm, function(data){
		if (data == 1) {
			$("#cbbNvAllowedFunc option:selected").remove();
			var option = document.createElement("option");
			option.value = perm;
		  	option.innerHTML = perm;
		  	select1.append(option);
		}
		$('#loading').hide();
	});
});

$('#nvRightToLeft').click(function() {
	var perm = $('#cbbNvDeniedFunc').val();
	if (perm == null)
		return;
	var select = $('#cbbNvAllowedFunc');
	
	var select1 = $('#cbbNvDeniedFunc');
	
	$('#loading').show();
	$.get('/phanquyen/json/?func=addQuyen&type=2&ma=' + $('#cbbNV').val() + '&perm=' + perm, function(data){
		if (data == 1) {
			$("#cbbNvDeniedFunc option:selected").remove();
			var option = document.createElement("option");
			option.value = perm;
		  	option.innerHTML = perm;
		  	select.append(option);
		}
		$('#loading').hide();
	});
});

$(document).ready(function() {
	
	$('#QuanLy').addClass('Active');
	$("#cbbNV option:first-child").attr("selected","selected");
	$('#cbbTenNV').change();
	$("#cbbLoaiNV option:first-child").attr("selected","selected");
	$('#cbbLoaiNV').change();
	$('#loading').hide();
});